import { Injectable, Inject } from '@angular/core';
import { HttpClient, HttpResponse, HttpHeaders } from '@angular/common/http';
import { Observable, of, Subject } from 'rxjs';
import { catchError, filter, map } from 'rxjs/operators';
import { ConfigService } from 'app/config/config.service';
import { EQUIPMENT_API_URL, ALARM_API_URL } from 'app/app.constants';

export const httpOptions: Object = {
    headers: new HttpHeaders({
        'Content-Type': 'application/json'
        // 'Authorization': 'my-auth-token',
        // Accept: 'text/plain'
    })
    // responseType: 'text'
};
@Injectable({
    providedIn: 'root'
})
export class DisseminationService {
    // private apiUrl_vms_online = 'http://192.168.0.56:9576';
    private apiUrl_vms_online = this.config.apiUrl.vms;
    public apiUrl_vms_uploadImg = this.config.apiUrl.vms + '/vnqnvmsmgt/uploadImage';
    public events: Event[] = [];
    typeObj = {
        VMS: 0,
        VD: 1,
        LPR: 2,
        CCTV: 3,
        LCS: 4,
        RMC: 5,
        TSC: 6,
        VSLS: 7,
        EPolice: 8
    };
    typeArr = ['VMS', 'VD', 'LPR', 'CCTV', 'LCS', 'RMC', 'TSC', 'VSLS', 'EPolice'];
    graphicTypeObj = {
        'monochrome 1bit': 1,
        'monochrome 8bit': 2,
        'color Classic': 3,
        'color 24bit': 4
    };
    graphicTypeArr = ['monochrome 1bit', 'monochrome 8bit', 'color Classic', 'color 24bit'];
    constructor(private http: HttpClient, private config: ConfigService) {}
    // message Library CRUD
    getMessageLibraryList(params): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/getVmsLibraryMessageByParam`;
        return this.http.post<any>(url, params, httpOptions);
    }
    createMessageLibrary(params): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/createVmsLibraryMessage`;
        return this.http.post<any>(url, params, httpOptions);
    }
    updateMessageLibrary(params): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/updateVmsLibraryMessage`;
        return this.http.post<any>(url, params, httpOptions);
    }
    /**
     * test api
     * @param params
     * @returns
     */
    createMessageLibraryByServer(params): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/createVmsLibraryMessageGenByServer`;
        return this.http.post<any>(url, params, httpOptions);
    }
    updateMessageLibraryByServer(params): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/updateVmsLibraryMessageGenByServer`;
        return this.http.post<any>(url, params, httpOptions);
    }
    /**test over */
    deleteMessageLibrary(msgId): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/deleteVmsLibraryMessage?msgId=` + msgId;
        return this.http.post<any>(url, {}, httpOptions);
    }
    // category CRUD
    getCategoryList(params): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/getVmsCategoryByParam`;
        return this.http.post<any>(url, params, httpOptions);
    }
    createCategory(params: { categoryId: string; categoryName: string }): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/createVmsCategory`;
        return this.http.post<any>(url, params, httpOptions);
    }
    updateCategory(params: { categoryId: string; categoryName: string }): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/updateVmsCategory`;
        return this.http.post<any>(url, params, httpOptions);
    }
    deleteCategory(categoryId: string): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/deleteVmsCategory?categoryId=` + categoryId;
        return this.http.post<any>(url, {});
    }
    // Graphic CRUD
    getGraphicList(params): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/getVmsGraphicByParam`;
        return this.http.post<any>(url, params, httpOptions);
    }
    createGraphic(params): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/createVmsGraphic`;
        return this.http.post<any>(url, params, httpOptions);
    }
    updateGraphic(params): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/updateVmsGraphic`;
        return this.http.post<any>(url, params, httpOptions);
    }
    deleteGraphic(graphicId): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/deleteVmsGraphic?graphicId=` + graphicId;
        return this.http.post<any>(url, {}, httpOptions);
    }
    deleteOriginImg(deleteUrl): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/deleteImage?url=${deleteUrl}`;
        return this.http.get<any>(url);
    }

    // template
    getTemplateList(params): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/getVmsTemplateByParam`;
        return this.http.post<any>(url, params, httpOptions);
    }
    getTemplateById(templateId): Observable<any> {
        const url = `${this.apiUrl_vms_online}/vnqnvmsmgt/getVmsTemplateById`;
        return this.http.get<any>(url, { params: { templateId } });
    }
    // type  0:转换为时间戳， 1:转换为显示时间
    changeDate(dates, type) {
        if (!dates) {
            return null;
        }
        let dated;
        switch (type) {
            case 0: {
                dated = new Date(dates).getTime();
                break;
            }
            case 1: {
                const temp = new Date(dates);
                const year = temp.getFullYear();
                const month = temp.getMonth() + 1 >= 10 ? temp.getMonth() + 1 : '0' + (temp.getMonth() + 1);
                const day = temp.getDate() >= 10 ? temp.getDate() : '0' + temp.getDate();
                const hours = temp.getHours() >= 10 ? temp.getHours() : '0' + temp.getHours();
                const min = temp.getMinutes() >= 10 ? temp.getMinutes() : '0' + temp.getMinutes();
                // dated = temp.getFullYear() + '-' + (temp.getMonth() + 1) + '-' + temp.getDate() + ' ' + temp.getHours() + ':' + temp.getMinutes();
                dated = year + '-' + month + '-' + day + ' ' + hours + ':' + min;
                break;
            }
        }
        return dated;
    }
    // 转换设备类型 字符串 编码
    changeEqtType(eqt) {
        if (typeof eqt === 'number') {
            return this.typeArr[eqt];
        } else {
            return this.typeObj[eqt];
        }
    }
    changeGrapicType(graphic) {
        if (typeof graphic === 'number') {
            return this.graphicTypeArr[graphic];
        } else {
            return this.graphicTypeObj[graphic];
        }
    }
}
